package com.example.day15;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

public class MyAdapter extends BaseAdapter {
    Context context;
    List<Map<String,Object>> list1;
    public MyAdapter(Context context, List<Map<String,Object>> list1){
        this.context=context;
        this.list1=list1;
    }
    @Override
    public int getCount() {
        return list1.size();
    }

    @Override
    public Object getItem(int position) {
        return list1.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder ;
        Log.e("###MyListViewBase", "getView " + position + " " + convertView);

        if (convertView == null) {
            convertView = View.inflate(context, R.layout.test_item, null);
            viewHolder = new ViewHolder();
            viewHolder.textView = convertView.findViewById(R.id.text1);
            viewHolder.textView1 = convertView.findViewById(R.id.text2);
            viewHolder.image = convertView.findViewById(R.id.image1);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();//取出ViewHolder对象
        }
        Map<String,Object> map= list1.get(position);
        if(viewHolder!=null){
            viewHolder.textView.setText(map.get("title").toString());
            viewHolder.textView1.setText(map.get("food_str").toString());
            viewHolder.image.setImageBitmap((Bitmap)map.get("pic"));
        }

        return convertView;
    }

    public class ViewHolder{
        TextView textView;
        TextView textView1;
        ImageView image;
    }
}
